<template>
  <div class="app-container">
    <el-form :inline="true" :model="formInline" ref="searchForm" class="demo-form-inline">
      <el-form-item>
        <el-button type="danger" size="small" @click="deleteBatch" icon="el-icon-delete">批量删除</el-button>
        <el-button type="warning" size="small" @click="handleAdd" icon="el-icon-plus">新增</el-button>
      </el-form-item>
      <el-form-item label="姓名">
        <el-input v-model="formInline.name" placeholder="姓名"></el-input>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="selectAll">查询</el-button>
        <el-button type="primary" plain size="small" @click="resetSearchForm('searchForm')" icon="el-icon-refresh">
          重置
        </el-button>
        <el-button type="primary" plain size="small" @click="dialogExcelVisible = true" icon="el-icon-upload2">导入
          </el-button>
          <el-button type="primary" plain size="small" @click="downloadExcel" icon="el-icon-download">导出</el-button>
      </el-form-item>
    </el-form>
    <!-- 上传excel的对话框 -->
            <el-dialog title="上传计量单位Excel" :visible.sync="dialogExcelVisible" width="40%">
                <el-upload class="upload-demo" drag action="http://localhost:8888/pm/users/import"
                    accept=".xlsx,.xls" :on-success="uploadExcelSuccess" :on-error="uploadExcelError" multiple>
                    <i class="el-icon-upload"></i>
                    <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
                    <div class="el-upload__tip" slot="tip">只能上传.xlsx,.xls文件，且不超过500kb</div>
                </el-upload>
            </el-dialog>

    <el-table :data="usersList" @selection-change="handleSelectionChange" style="width: 100%;  margin-bottom: 20px"
      row-key="id" border default-expand-all :tree-props="{ children: 'children' }">
      <!-- <el-table-column prop="id" label="id" sortable width="180">
              </el-table-column> -->
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column prop="id" label="序号" sortable width="180">
      </el-table-column>
      <el-table-column prop="username" label="用户名" sortable width="180">
      </el-table-column>
      <el-table-column prop="name" label="姓名" sortable width="180">
      </el-table-column>
      <el-table-column prop="troles.name" label="角色" sortable width="180">
      </el-table-column>
  
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit"
            @click="handleUpdate('编辑用户', scope.$index, scope.row)">编辑</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="currentPage"
      :page-sizes="[5, 10, 15, 20]" :page-size="pageSize" layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
    <!-- </div> -->

    <!-- </el-row> -->
    <!-- dialog    -->
    <el-dialog :title="title" :visible.sync="dialogFormVisible">
      <el-form :model="form">
        <el-form-item label="用户名" :label-width="formLabelWidth">
          <el-input v-model="form.username" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="密码" :label-width="formLabelWidth">
          <el-input v-model="form.password" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="姓名" :label-width="formLabelWidth">
          <el-input v-model="form.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="角色" :label-width="formLabelWidth">
          <el-select v-model="form.rid" placeholder="请选择">
            <el-option v-for="item in trolesList" :key="item.id" :label="item.name" :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="addUsers()">确 定</el-button>
      </div>
    </el-dialog>

  </div>
</template>
<script>
import { users,addUsers, updateUsers, deleteById, deleteByIds } from '@/api/users'
import { trolesList } from '@/api/roles'
export default {
  name: 'Users',
  data() {
    return {
      usersList: [],
      currentPage: 1,
      pageSize: 5,
      total: 0,
      dialogFormVisible: false,
      form: {
        id: '',
        username: '',
        name: '',
        rid: '',
      },
      formInline: {
        name: ''
      },
      trolesList: [],
      ids: [],
      title: '添加用户',
      dialogExcelVisible: false,
    }
  },
  methods: {
    list() {
      users({ pageNum: this.currentPage, pageSize: this.pageSize, name: this.formInline.name }).then(res => {
        this.usersList = res.data.list
        this.total = res.data.total
      })
    },
    handleCurrentChange(val) {
      this.currentPage = val
      this.list()
    },
    handleSizeChange(val) {
      this.pageSize = val
      this.list()
    },
    selectAll() {
      this.currentPage = 1
      this.list()
    },
    resetSearchForm(formName) {
      this.$refs[formName].resetFields()
      this.currentPage = 1
      this.formInline.name = ''
      this.list()
    },
    handleAdd() {
      this.title = '添加用户'
      this.dialogFormVisible = true
      trolesList().then(res => {
        console.log("line139----------", res)
        this.trolesList = res.data
        console.log("line141----------", this.trolesList)
        this.dialogFormVisible = true
        this.form = {}
      })

    },
    addUsers() {
      console.log("line159----------", this.title)
      if (this.title == "添加用户") {
        console.log("line161----------", this.form)
        addUsers(this.form).then(res => {
          if (res.code == 200) {
            this.$message({
              type: 'info',
              message: '添加成功!'
            })
            this.dialogFormVisible = false
            this.list()
          }
        })
      }
      if (this.title == "编辑用户") {
        updateUsers(this.form).then(res => {
          if (res.code == 200) {
            this.$message({
              type: 'info',
              message: '编辑成功!'
            })
            this.dialogFormVisible = false
            this.list()
          }
        })
      }

    },
    handleUpdate(title, index, row) {
      console.log("line186----------", row)
      this.title = title
      trolesList().then(res => {
        this.trolesList = res.data
        this.dialogFormVisible = true
      })
      this.dialogFormVisible = true
      this.form = row
    },
    handleDelete(row) {
      console.log("line157----------", row)
      this.$confirm('此操作将永久删除id为' + row.id + '的客户, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteById(row.id).then(() => {
          if (row.code == 200) {
            this.$message({
              type: 'info',
              message: '删除成功!'
            })
          }
        })
        this.list()
      }).catch(() => {
        this.list()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    handleSelectionChange(val) {
      this.ids = [];
      for (let i = 0; i < val.length; i++) {
        this.ids.push(val[i].id);
      }
    },
    deleteBatch() {
      this.$confirm('此操作将永久删除选中的' + this.ids.length + '个客户, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteByIds(this.ids).then(() => {
          this.$message({
            type: 'info',
            message: '删除成功!'
          })
          this.list()
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    uploadExcelSuccess(res, file) {
      this.$message({
        type: 'info',
        message: '上传成功!'
      })
      this.dialogExcelVisible = false
      this.list()
    },
    downloadExcel() {
      window.open("http://localhost:8888/pm/users/export")
    }
  },
  created() {
    this.list()
  },
}

</script>
<style scoped></style>
